<template>
  <el-dialog :title="buildingName+'套餐详情'" :visible.sync="showDialog" width="60%" center :before-close="closeThisDialog"
             append-to-body
  >
    <div style="padding: 10px 20px">
      <el-table :data="comboDataList" border size="mini" stripe :summary-method="getSummaries" show-summary>
        <el-table-column label="套餐名称" prop="name" width="200" align="center"></el-table-column>
        <el-table-column v-for="item in comboPointTypeArr" min-width="100" :key="item.id" :label="item.name"
                         align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.pointDataMap[item.id].need_num }}
          </template>
        </el-table-column>
        <el-table-column label="分配数" width="160" align="center">
          <template slot-scope="scope" style="width: 140px">
            <el-input-number size="mini" v-model="scope.row.distribution_num" :min="0" :step="1" step-strictly
                             :controls="false"
            ></el-input-number>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeThisDialog" size="mini">取 消</el-button>
      <el-button type="primary" @click="save" size="mini">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'comboDistributeDialog',
  props: {
    // 点位数据
    /**
     *
     {
      combo_id:套餐ID
      rule_id:组合ID
      name:XX套餐-XX组合
      pointDataMap:{
        5:{   // 点位类型为key
            need_num:2, 组合所需要的数量
            total_num:2 点位可用数的总量
          }
      }
     distribution_num:分配数
   }
     */
    comboDataList: {
      type: Array,
      default() {
        return []
      }
    },
    // 楼盘名称
    buildingName: {
      type: String,
      default: null
    },
    showDialog: {
      type: Boolean,
      default: false
    },
    // 点位类型数据
    comboPointTypeArr: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      combo_data_list: []
    }
  },
  methods: {
    closeThisDialog() {
      this.$emit('shut')
    },
    save() {
      let map = new Map()// 所有点位总数
      let use_count_map = new Map() // 所有使用点位总数
      this.combo_data_list.forEach(item => {
        for (let key in item.pointDataMap) {
          if (!map.has(key)) {
            map.set(key, item.pointDataMap[key].total_num)
          }
          if (use_count_map.has(key)) {
            let t = use_count_map.get(key)
            let use_num = item.pointDataMap[key].need_num * item.distribution_num
            use_count_map.set(key, t + use_num)
          } else {
            let use_num = item.pointDataMap[key].need_num * item.distribution_num
            use_count_map.set(key, use_num)
          }
        }
      })
      let keys = map.keys()
      for (let key of keys) {
        if (use_count_map.get(key) > map.get(key)) {
          let find = this.comboPointTypeArr.find(item => item.id == key)
          this.$message.error({
            message: find.name + '分配数大于总数,请调整套餐数量',
            duration: 5000
          })
          return
        }
      }
      this.$emit('save', this.combo_data_list, use_count_map)
    },
    /**
     * 合计
     * @param param
     * @returns {*[]}
     */
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        } else if (index != columns.length - 1) {
          let key = this.comboPointTypeArr[index - 1].id
          let sum = 0
          let use_num = 0
          data.forEach(item => {
            sum = item.pointDataMap[key].total_num
            let n = isNaN(item.distribution_num) ? 0 : item.distribution_num
            use_num += (item.pointDataMap[key].need_num * n)
          })
          sums[index] = use_num + '/' + sum
        } else {
          let sum = 0
          data.forEach(item => {
            let n = isNaN(item.distribution_num) ? 0 : item.distribution_num
            sum += n
          })
          sums[index] = sum
        }
      })
      return sums
    }
  },
  watch: {
    comboDataList: {
      handler(comboDataList) {
        this.combo_data_list = comboDataList.map(item => {
          return Object.assign({}, item)
        })
      },
      deep: true
    }
  }
}
</script>

<style scoped>

</style>
